package cn.abyss.admin.ums.service.impl;

import cn.abyss.admin.consts.AdminWebConstants;
import cn.abyss.admin.enums.AdminWebExceptionEnum;
import cn.abyss.admin.enums.BaseAdminWebExceptionLevelEnum;
import cn.abyss.admin.exception.AdminWebException;
import cn.abyss.admin.ums.dao.UmsAdminRoleRelationMapper;
import cn.abyss.admin.ums.model.UmsAdmin;
import cn.abyss.admin.ums.model.UmsAdminRoleRelation;
import cn.abyss.admin.ums.model.UmsResource;
import cn.abyss.admin.ums.service.UmsAdminRoleRelationService;
import cn.abyss.admin.util.AbyssAdminDataValidator;
import cn.abyss.admin.util.ResultMap;
import cn.hutool.core.map.MapUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 *
 * @author zhangsj
 * @date Created on 2021/8/26
 */
@Service
public class UmsAdminRoleRelationServiceImpl implements UmsAdminRoleRelationService {
    @Autowired
    private UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    @Override
    public void insert(UmsAdminRoleRelation umsAdminRoleRelation) throws Exception {
        int row = umsAdminRoleRelationMapper.insertUseGeneratedKeys(umsAdminRoleRelation);
        if (row != 1) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_ROLE_RELATION_INSERT_FAIL, BaseAdminWebExceptionLevelEnum.INTERNAL_EXCEPTION);
        }
    }

    @Override
    public void update(UmsAdminRoleRelation umsAdminRoleRelation) throws Exception {
        int row = umsAdminRoleRelationMapper.updateByPrimaryKeySelective(umsAdminRoleRelation);
        if (row != 1) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_ROLE_RELATION_UPDATE_FAIL, BaseAdminWebExceptionLevelEnum.INTERNAL_EXCEPTION);
        }
    }

    @Override
    public void delete(Long id) throws Exception {
        int row = umsAdminRoleRelationMapper.deleteByPrimaryKey(id);
        if (row != 1) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_ROLE_RELATION_DELETE_FAIL, BaseAdminWebExceptionLevelEnum.INTERNAL_EXCEPTION);
        }
    }

    @Override
    public UmsAdminRoleRelation getById(Long id) throws Exception {
        return umsAdminRoleRelationMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> listForPage(Map<String, Object> map, int pageNum, int pageSize) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(pageNum, pageSize, true);
        List<ResultMap> list = umsAdminRoleRelationMapper.queryUmsAdminRoleRelationMap(map);
        PageInfo<ResultMap> page = new PageInfo<>(list);
        resultMap.put("pageSize", page.getPageSize());
        resultMap.put("pageNum", page.getPageNum());
        resultMap.put("total", page.getTotal());
        resultMap.put("list", list);
        return resultMap;
    }

    @Override
    public List<UmsAdminRoleRelation> query(Map<String, Object> map) throws Exception {
        Example example = new Example(UmsAdminRoleRelation.class);
        Example.Criteria criteria = example.createCriteria();
        if (!AbyssAdminDataValidator.isEmptyId((Long) map.get("id"))) {
            criteria.andEqualTo("id", map.get("id"));
        }
        if (!AbyssAdminDataValidator.isEmptyId((Long) map.get("adminId"))) {
            criteria.andEqualTo("adminId", map.get("adminId"));
        }
        if (!StringUtils.isEmpty(map.get("roleId"))) {
            criteria.andEqualTo("roleId", map.get("roleId"));
        }
        if (null != map.get("roleIdList")) {
            List<Long> roleIdList = (List<Long>) map.get("roleIdList");
            criteria.andIn("roleId", roleIdList);
        }
        return umsAdminRoleRelationMapper.selectByExample(example);
    }

    @Override
    public ResultMap getSummation(Map<String, Object> map)  throws Exception {
        List<ResultMap> list = umsAdminRoleRelationMapper.sumUmsAdminRoleRelation(map);
        if(list == null || list.size() != 1){
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_SUM_QUERY_FAIL, BaseAdminWebExceptionLevelEnum.INTERNAL_EXCEPTION);
        }
        return list.get(0);
    }

    @Override
    public List<ResultMap> listForUmsInfo(Map<String, Object> map) throws Exception {
        List<ResultMap> list = umsAdminRoleRelationMapper.queryUmsAdminRoleRelationMap(map);
        return list;
    }

    @Override
    public List<UmsResource> queryResourceList(Map<String, Object> map) {
        return umsAdminRoleRelationMapper.queryResourceList(map);
    }
}
